﻿Imports Aricie.DNN.Modules.LuceneSearch.ModuleProviders
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Common.Utilities
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports BizModules.UltraPhotoGallery
Imports System.Text.RegularExpressions

Namespace Aricie.DNN.Modules.LuceneSearch.ModuleProviders

    Public Class UMGProvider
        Implements ILuceneSearchableUpgrade

        Private Shared PhotoIdExtractor As New Regex("PhotoId=(?<PhotoId>\d+)", RegexOptions.Compiled)
        Private Shared AlbumIdExtractor As New Regex("AlbumId=(?<AlbumId>\d+)", RegexOptions.Compiled)

        Public Sub UpgradeSearchItem(ByVal portalId As Integer, ByRef searchItem As Aricie.DNN.Modules.LuceneSearch.Business.LuceneSearchItemInfo) Implements Aricie.DNN.Modules.LuceneSearch.ModuleProviders.ILuceneSearchableUpgrade.UpgradeSearchItem

            Dim matchObj As Match = PhotoIdExtractor.Match(searchItem.SearchKey)
            If matchObj.Success Then
                Dim PhotoId As Integer = Integer.Parse(matchObj.Groups("PhotoId").Value)
                Dim CurrentPhotoInfo As PhotoInfo = PhotoController.Get(PhotoId)
                For Each tag As TagInfo In CurrentPhotoInfo.Tags
                    searchItem.ContentTaxonomy.Add(tag.Tag.Trim())
                Next

                Dim AlbumTitlesList As New List(Of String)

                Dim CurrentAlbumInfoId As Integer = CurrentPhotoInfo.AlbumId
                While CurrentAlbumInfoId <> -1
                    Dim CurrentAlbumInfo As AlbumInfo = AlbumController.Get(CurrentAlbumInfoId)

                    If (CurrentAlbumInfo IsNot Nothing) Then
                        AlbumTitlesList.Add(CurrentAlbumInfo.Title.Trim())
                        CurrentAlbumInfoId = CurrentAlbumInfo.ParentId
                    Else
                        CurrentAlbumInfoId = -1
                    End If
                End While
                If AlbumTitlesList.Count > 0 Then
                    AlbumTitlesList.Reverse()
                    searchItem.ContentTaxonomy.Add(String.Join(" > ", AlbumTitlesList.ToArray))
                End If

                searchItem.ItemType = DirectCast([Enum].Parse(GetType(MediaType), CurrentPhotoInfo.MediaType), MediaType).ToString()

                ' on indique qu'on indexe pas cet élément s'il n'est pas approuvé, ou si la date d'indexation sort de ses dates de publication
                searchItem.CanBeReturnedBySearch = CurrentPhotoInfo.Approved

            Else
            matchObj = AlbumIdExtractor.Match(searchItem.SearchKey)
            If matchObj.Success Then
                searchItem.ItemType = "Album"
            End If
            ' mais de toute manière, nous skippons les éléments de type album pour IBS
            searchItem.IndexingStatus = IndexingItemStatus.Skipped
            End If

        End Sub

    End Class

End Namespace